package com.itheimamobileguard.activitys;

import java.util.List;

import com.example.mobile.R;
import com.itheimamobileguard.db.dao.BlackNumberDao;
import com.itheimamobileguard.domain.BlockNumberInfo;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CallSmsSafeActivity2 extends Activity {
	private ListView lv_callsms_safe;
	private BlackNumberDao dao;
	private List<BlockNumberInfo> infos;
	private LinearLayout ll_add_number_tips;
	private LinearLayout ll_loading;
	/**
	 * 页码输入框
	 */
	private EditText et_page_number;

	/**
	 * 页码信息
	 */
	private TextView tv_page_info;
	/**
	 * 页面大小
	 */
	private static final int pageSize = 20;
	/**
	 * 当前页码号
	 */
	private int currentPageNumber = 1;
	/**
	 * 总页数
	 */
	private int totalPage;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			ll_loading.setVisibility(View.INVISIBLE);
			if (infos.size() == 0) {
				// 没有数据，显示添加数据的提醒
				ll_add_number_tips.setVisibility(View.VISIBLE);
			} else {
				// 显示数据
				lv_callsms_safe.setAdapter(new CallSmsSafeAdapter());
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initUI();
		fillData();
	}

	/**
	 * 填充数据
	 */
	private void fillData() {
		dao = new BlackNumberDao(this);
		// 数据库总条目个数/每个页面最多显示的数据
		totalPage = dao.getTotalNumber() / pageSize;
		tv_page_info.setText(currentPageNumber+"/" + totalPage);
		// 耗时的操作，应该放在子线程里面执行
		ll_loading.setVisibility(View.VISIBLE);
		new Thread() {
			public void run() {
				infos = dao.findPart(currentPageNumber, pageSize);
				handler.sendEmptyMessage(0);
			}
		}.start();
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		setContentView(R.layout.activity_callsms_safe2);
		ll_add_number_tips = (LinearLayout) findViewById(R.id.ll_add_number_tips);
		lv_callsms_safe = (ListView) findViewById(R.id.lv_callsms_safe);
		ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
		et_page_number = (EditText) findViewById(R.id.et_page_number);
		tv_page_info = (TextView) findViewById(R.id.tv_page_info);
	}

	public class CallSmsSafeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return infos.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			if (convertView == null) {
				view = View.inflate(CallSmsSafeActivity2.this,
						R.layout.item_callsms, null);
			} else {
				view = convertView;
			}
			TextView tv_phone = (TextView) view
					.findViewById(R.id.tv_item_phone);
			TextView tv_mode = (TextView) view.findViewById(R.id.tv_item_mode);
			BlockNumberInfo info = infos.get(position);
			tv_phone.setText(info.getNumber());
			String mode = info.getMode();
			if ("1".equals(mode)) {
				tv_mode.setText("全部拦截");
			} else if ("2".equals(mode)) {
				tv_mode.setText("短信拦截");
			} else if ("3".equals("电话拦截")) {
				tv_mode.setText("电话拦截");
			}
			return view;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
	
	
	/**
	 * 上一页
	 * @param view
	 */
	public void prePage(View view) {
		if (currentPageNumber <=1) {
			Toast.makeText(this, "已经是第一页", 0).show();
			return;
		}
		currentPageNumber--;
		fillData();
	}
	/**
	 * 下一页
	 * @param view
	 */
	public void nextPage(View view) {
		if (currentPageNumber>=totalPage) {
			Toast.makeText(this, "已经是最后一页", 0).show();
			return;
		}
		currentPageNumber++;
		fillData();
	}
	/**
	 * 跳转
	 * @param view
	 */
	public void jump(View view) {
		String str_pageNumber = et_page_number.getText().toString().trim();
		if (TextUtils.isEmpty(str_pageNumber)) {
			Toast.makeText(this, "请输入页码号", 0).show();
		}else{
			int number = Integer.parseInt(str_pageNumber);
			if(number>0&&number<=10){
				currentPageNumber = number;
				fillData();
			}else {
				Toast.makeText(this, "请输入正确的页码号", 0).show();
			}
		}
	}
}
